<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
var echarts = require('echarts/lib/echarts')
// 引入 ECharts 主模块
require('echarts/lib/chart/pie')
// 引入提示框和标题组件
require('echarts/lib/component/tooltip')
require('echarts/lib/component/title')
require('echarts/lib/component/legend')
// require('echarts/theme/macarons') // echarts theme
import { debounce } from '@/utils'
import { themeColor } from '@/assets/echarts/theme'

export default {
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '150px'
    },
    height: {
      type: String,
      default: '150px'
    },
    total: {
      type: Number,
      default: 0
    },
    itemNum: {
      type: Number,
      default: 0
    },
    statusName: {
      type: String,
      default: ''
    },
    color: {
      type: String,
      default: ''
    },
    statusCountList: {
      type: Array,
      default: () => ([])
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    'statusCountList': function() {
      this.initChart()
    }
  },
  mounted() {
    this.initChart()
    this.__resizeHandler = debounce(() => {
      if (this.chart) {
        this.chart.resize()
      }
    }, 100)
    window.addEventListener('resize', this.__resizeHandler)
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    window.removeEventListener('resize', this.__resizeHandler)
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    setName(val) {
      const titleItem = {
        'RUN': '生产数量',
        'ALARM': '报警数量',
        'IDLE': '空闲数量',
        'NETWORKFAILURE': '断网数量',
        'STOP': '停机数量'
      }
      return titleItem[val]
    },
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.chart.setOption({
        tooltip: {
          trigger: 'item',
          formatter: '{a} <br/>{b}: {c} ({d}%)',
          position: [10, 20]
        },
        color: themeColor,
        series: [
          {
            name: this.setName(this.statusName),
            type: 'pie',
            radius: ['60%', '80%'],
            avoidLabelOverlap: false,
            label: {
              show: false,
              position: 'center'
            },
            labelLine: {
              show: false
            },
            data: [
              {
                value: this.itemNum,
                name: this.setName(this.statusName).replace('数量', ''),
                itemStyle: {
                  normal: {
                    color: this.color
                  }
                },
                label: {
                  show: true,
                  position: 'center',
                  color: '#333',
                  formatter: (val) => {
                    return `${val.value}/${this.total}(${val.percent}%)`
                  }
                }
              },
              {
                value: this.total - this.itemNum,
                name: '合计',
                hoverAnimation: false,
                tooltip: {
                  show: false
                },
                itemStyle: {
                  color: '#ccc'
                }
              }
            ]
          }
        ]
      })
    }
  }
}
</script>
